package stack;

import java.util.Stack;

public class ValidateStackSequences {
    public static boolean code(int[] pushed, int[] popped) {
        Stack<Integer> stack = new Stack<>();
        int j = 0;

        for (int i = 0; i < pushed.length; i++){
            stack.push(pushed[i]);
            while (!stack.isEmpty() && stack.peek() == popped[j]){
                stack.pop();
                j++;
            }
        }

        if (j != popped.length){
            return false;
        }else {
            return true;
        }
    }

    public static void main(String[] args) {
        ValidateStackSequences.code(new int[]{1, 2, 3, 4, 5}, new int[]{5, 4, 3, 2, 1});
    }
}
